import instacne from "../request";
import type { Ref } from "vue";

/**
 * /home/newProduct/list
 * 获取新品推荐数据
 */
interface INewProductListParams {
  productName?: string | Ref<string>;
  recommendStatus?: number | Ref<number>;
  pageNum?: number | Ref<number>;
  pageSize?: number | Ref<number>;
}

interface INewProductListItem {
  id: number;
  productId: number;
  productName: string;
  recommendStatus: number;
  sort: number;
}

interface INewProductListRes {
  list: INewProductListItem[];
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

interface IBrandAdd {
  brandId: number;
  brandName: string;
  id: number;
  recommendStatus: number;
  sort: number;
}

const getNewProductList = (
  data?: INewProductListParams
): Promise<INewProductListRes> => {
  return instacne.get("/home/recommendProduct/list", {
    params: data,
  });
};

// 删除品牌数据
const deleteRecommendProduct = (data: number): Promise<IBrandAdd> => {
  return instacne.get(`/home/recommendProduct/delete/${data}`);
};
/*
 * 修改状态
 */
const updataStatusProduct = (params: {
  recommendStatus: number | Ref<number>;
  id: any;
}) =>
  instacne.post(
    `/home/recommendProduct/update/recommendStatus/${params.id}?status=${params.recommendStatus}`
  );

// 修改品牌推荐排序 /home/recommendProduct/update/sort/{id}
interface HomeBrandUpdateSortData {
  id: number;
  sort: number;
}

const HomeRecommendProductUpdateSortDataApi = (
  data: HomeBrandUpdateSortData
) => {
  return instacne.post(
    `/home/recommendProduct/update/sort/${data.id}?sort=${data.sort}&id=${data.id}`
  );
};

// 删除 /home/brand/delete
interface HomeBrandDeleteData {
  ids: number;
}

const HomeRecommendProductDeleteDataApi = (data: HomeBrandDeleteData) => {
  return instacne.post("/home/recommendProduct/delete", { data });
};

// 筛选数据请求 /recommendProduct/list
const recommendProductListApi = (
  data: INewProductListParams
): Promise<INewProductListRes> => {
  return instacne.get(
    `/recommendProduct/list?pageNum=${data.pageNum}&pageSize=${data.pageSize}`
  );
};

// 筛选数据添加 /home/recommendProduct/create

let homeRecommendProductCreate = (data: object) => {
  return instacne.post("/home/recommendProduct/create", data);
};
// 搜索
const homeRecommendProductSearchApi = (
  data: INewProductListParams
): Promise<INewProductListRes> => {
  return instacne.get(
    `/home/recommendProduct/list?pageNum=${data.pageNum}&pageSize=${data.pageSize}&productName=${data.productName}`
  );
};
export {
  recommendProductListApi,
  homeRecommendProductCreate,
  getNewProductList,
  deleteRecommendProduct,
  updataStatusProduct,
  HomeRecommendProductUpdateSortDataApi,
  HomeRecommendProductDeleteDataApi,
  homeRecommendProductSearchApi,
};
export type { INewProductListItem };
